package com.itheima.health.util;


import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;



/**
 * @Author: zhang
 * @Date: 2022/08/01/20:51
 * @Description:
 */

/**
 * Poi读excel文件工具类
 */
public class POIUtils {
    /**
     * 读取excel数据
     *
     * @throws IOException
     */
    private final static String xlsx = "xlsx";
    private final static String DATE_FORMAT = "yyyy/MM/dd";

    public static List<String[]> readExcel(MultipartFile excelFile) throws IOException {
        checkFile(excelFile);

        //获取工作簿对象
        InputStream in = excelFile.getInputStream();
        XSSFWorkbook workbook = new XSSFWorkbook(in);
        //创建返回对象
        List<String[]> list = new ArrayList<>();
        //把每行中的值作为一个数组，所有行作为一个集合返回
        if (workbook != null) {
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                //获取当前工作表
                XSSFSheet sheet = workbook.getSheetAt(i);
                if (sheet == null) {
                    continue;
                }
                //获取当前sheet的开始行
                int firstRowNum = sheet.getFirstRowNum();
                //获取结束行
                int lastRowNum = sheet.getLastRowNum();
                //循环除了第一行的所有行
                for (int j = firstRowNum + 1; j <= lastRowNum; j++) {
                    //获取当前行
                    Row row = sheet.getRow(j);
                    if (row == null) {
                        continue;
                    }
                    //获取当前行的开始列
                    short firstCellNum = row.getFirstCellNum();
                    //获取当前行的列数
                    int lastCellNum = row.getPhysicalNumberOfCells();
                    String[] cells = new String[row.getPhysicalNumberOfCells()];
                    //循环当前行
                    for (int k = firstCellNum; k < lastCellNum; k++) {
                        Cell cell = row.getCell(k);
                        cells[k] = getCellValue(cell);
                    }
                    list.add(cells);
                }

            }
            workbook.close();
            in.close();
        }
        return list;
    }

    /**
     * 检查文件
     */
    public static void checkFile(MultipartFile excelFile) throws IOException {
        //判断文件是否存在
        if (excelFile == null) {
            throw new FileNotFoundException("文件不存在..");
        }
        //判断文件名
        String filename = excelFile.getOriginalFilename();
        if (!filename.endsWith(xlsx)) {
            throw new IOException(filename + "不是excel文件");
        }


    }

    /**
     * 单元格内容格式处理
     */
    public static String getCellValue(Cell cell){
        String cellValue = "";
        if (cell == null){
            return cellValue;
        }
        //如果单元格为日期内型
        String dataFormatString = cell.getCellStyle().getDataFormatString();
        if (dataFormatString.equals("m/d/yy")){
            cellValue = new SimpleDateFormat(DATE_FORMAT).format(cell.getDateCellValue());
            return cellValue;
        }
        //把数字当成String来读
        if (cell.getCellType() ==Cell.CELL_TYPE_NUMERIC){
            cell.setCellType(Cell.CELL_TYPE_STRING);
        }
        //判断数据类型
        switch (cell.getCellType()){
            case Cell.CELL_TYPE_NUMERIC: //数字
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_STRING: //字符串
                cellValue = String.valueOf(cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN: //Boolean
                cellValue =  String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA: //公式
                 cellValue = String.valueOf(cell.getCellFormula());
                 break;
            case Cell.CELL_TYPE_BLANK: //空值
                cellValue ="";
                break;
            case Cell.CELL_TYPE_ERROR: //故障
                 cellValue = "非法字符";
                 break;
            default:
                cellValue = "未知类型";
                break;
        }
        return cellValue;
    }
}
